package com.longrise.filedownloader;

import android.os.Environment;

import java.io.File;


/**
 * Created by callxkj on 16/9/22.
 * 判断手机SD卡大小
 */
public class ExternalStorageUtils {

    private static final String TAG = "ExternalStorageUtils";

    public static boolean isCanStore(long fileLength){
        boolean result = false;
        if(Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())){
            File externalStorageFile = Environment.getExternalStorageDirectory();
            //long totalSpace = externalStorageFile.getTotalSpace();
            long usableSpace = externalStorageFile.getUsableSpace();
            if(fileLength>=0 && (usableSpace - 20971520) > fileLength ){ //可用的减去20M 然后和需要空间比较 20971520
               result = true;
            }
        }
        return result;
    }

    /**
     *
     * @return true 需要提示内存不足
     */
   public static boolean isShouldTips(){
       boolean result = false;
       if(Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())){
           File externalStorageFile = Environment.getExternalStorageDirectory();
           //long totalSpace = externalStorageFile.getTotalSpace();
           long usableSpace = externalStorageFile.getUsableSpace();

           if(usableSpace < 104857600){  //只有100M的时候提示
               result = true;
           }
       }
       return result;
   }
    /**
     * 大小转换 B KB M G
     */
    private static String FormetFileSize(long fileS) {// 转换文件大小
//		DecimalFormat df = new DecimalFormat("#.00");
        String fileSizeString = "";
        if (fileS < 1024) {
            fileSizeString = String.format("%.2f", (double) fileS) + "B";
        } else if (fileS < 1048576) {
            fileSizeString = String.format("%.2f", (double) fileS / 1024) + "K";
        } else if (fileS < 1073741824) {
            fileSizeString = String.format("%.2f", (double) fileS / 1048576) + "M";
        } else {
            fileSizeString = String.format("%.2f", (double) fileS / 1073741824) + "G";
        }
        if (fileSizeString.equals("0.00B")) {
            return "0B";
        }
        return fileSizeString;
    }
}
